package com.funtl.screw.admin.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funtl.screw.admin.dto.SysOauthClientDetailsDTO;
import com.funtl.screw.admin.entity.SysOauthClientDetails;
import com.funtl.screw.admin.config.ClientDetailsInit;
import com.funtl.screw.admin.mapper.SysOauthClientDetailsMapper;
import com.funtl.screw.admin.service.SysOauthClientDetailsService;
import com.funtl.screw.common.core.constant.CacheConstants;
import com.funtl.screw.common.core.constant.CommonConstants;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.core.util.SpringContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lusifer
 * @since 2018-05-15
 */
@Service
@RequiredArgsConstructor
public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClientDetailsMapper, SysOauthClientDetails>
        implements SysOauthClientDetailsService {

    /**
     * 通过ID删除客户端
     */
    @Override
    @CacheEvict(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId")
    public Boolean removeByClientId(String clientId) {
        // 更新库
        baseMapper.delete(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId));
        // 更新Redis
        SpringContextHolder.publishEvent(new ClientDetailsInit.ClientDetailsInitEvent(clientId));
        return Boolean.TRUE;
    }

    /**
     * 根据客户端信息
     */
    @Override
    @CacheEvict(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientDetailsDTO.clientId")
    public Boolean updateClientById(SysOauthClientDetailsDTO clientDetailsDTO) {
        // copy dto 对象
        SysOauthClientDetails clientDetails = new SysOauthClientDetails();
        BeanUtils.copyProperties(clientDetailsDTO, clientDetails);

        // 获取扩展信息,插入开关相关
        String information = clientDetailsDTO.getAdditionalInformation();
        JSONObject informationObj = JSONUtil.parseObj(information)
                .set(CommonConstants.CAPTCHA_FLAG, clientDetailsDTO.getCaptchaFlag());
        clientDetails.setAdditionalInformation(informationObj.toString());

        // 更新数据库
        baseMapper.updateById(clientDetails);
        // 更新Redis
        SpringContextHolder.publishEvent(new ClientDetailsInit.ClientDetailsInitEvent(clientDetails));
        return Boolean.TRUE;
    }

    /**
     * 添加客户端
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveClient(SysOauthClientDetailsDTO clientDetailsDTO) {
        // copy dto 对象
        SysOauthClientDetails clientDetails = new SysOauthClientDetails();
        BeanUtils.copyProperties(clientDetailsDTO, clientDetails);

        // 获取扩展信息,插入开关相关
        String information = clientDetailsDTO.getAdditionalInformation();
        JSONUtil.parseObj(information).set(CommonConstants.CAPTCHA_FLAG, clientDetailsDTO.getCaptchaFlag());

        // 插入数据
        this.baseMapper.insert(clientDetails);
        // 更新Redis
        SpringContextHolder.publishEvent(new ClientDetailsInit.ClientDetailsInitEvent(clientDetails));
        return Boolean.TRUE;
    }

    /**
     * 分页查询客户端信息
     */
    @Override
    public Page<SysOauthClientDetailsDTO> queryPage(QueryRequest queryRequest, SysOauthClientDetails query) {
        Page<SysOauthClientDetails> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        Page<SysOauthClientDetails> selectPage = baseMapper.selectPage(page, Wrappers.query(query));

        // 处理扩展字段组装dto
        List<SysOauthClientDetailsDTO> collect = selectPage.getRecords().stream().map(details -> {
            String information = details.getAdditionalInformation();
            String captchaFlag = JSONUtil.parseObj(information).getStr(CommonConstants.CAPTCHA_FLAG);
            SysOauthClientDetailsDTO dto = new SysOauthClientDetailsDTO();
            BeanUtils.copyProperties(details, dto);
            dto.setCaptchaFlag(captchaFlag);
            return dto;
        }).collect(Collectors.toList());

        // 构建dto page 对象
        Page<SysOauthClientDetailsDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), selectPage.getTotal());
        dtoPage.setRecords(collect);
        return dtoPage;
    }

    @Override
    @CacheEvict(value = CacheConstants.CLIENT_DETAILS_KEY, allEntries = true)
    public ResponseResult<Void> syncClientCache() {
        return ResponseResult.ok();
    }
}
